<template lang="">
    <div>
        <el-descriptions title="发布信息" direction="vertical" :column="4" border>
            <el-descriptions-item label="问卷名">{{conInfo.conTitle}}</el-descriptions-item>
            <el-descriptions-item label="问卷类型">{{$store.state.questionnaireType == 1 ? '评教问卷' : '普通问卷'}}
            </el-descriptions-item>
            <el-descriptions-item label="问卷编号">{{conInfo.conId}}</el-descriptions-item>
            <el-descriptions-item label="问卷描述" :span="2">{{conInfo.conDescribe}}</el-descriptions-item>
            <el-descriptions-item label="发布院系"><span v-for="item in select.department">{{item}} &nbsp;&nbsp;&nbsp;
                </span> </el-descriptions-item>
            <el-descriptions-item label="发布专业"><span v-if="!($store.state.isEmitAll)"
                    v-for="item in select.major">{{item}} &nbsp;&nbsp;&nbsp; </span>
                <span v-if="$store.state.isEmitAll">所选年级、学院的全体专业</span>
            </el-descriptions-item>
            <el-descriptions-item label="发布年级"><span v-for="item in select.grade">{{item}} &nbsp;&nbsp;&nbsp; </span>
            </el-descriptions-item>
            <el-descriptions-item label="发布班级"><span v-if="!($store.state.isEmitAll)"
                    v-for="item in select.class">{{item}} &nbsp;&nbsp;&nbsp; </span>
                <span v-if="$store.state.isEmitAll">所选年级、学院的全体班级</span>
            </el-descriptions-item>
        </el-descriptions>
        <div class="button-group-emit">
            <el-button type="primary" @click="confirmEmit()">确定发布</el-button>
        </div>
    </div>
</template>
<script>
    export default {
        name: 'Step4',
        data() {
            return {
                conInfo: {},
                select: {
                    grade: [],
                    department: [],
                    major: [],
                    class: []
                }
            }
        },
        methods: {
            confirmEmit() {
                let select = this.$store.state.userSelected
                this.$store.commit('loading', true)
                axios({
                    method: 'POST',
                    url: '/question/quePublish',
                    withCredentials: true,
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    data: JSON.stringify({
                        pubConid: Number(this.conInfo.conId),
                        pubGradeId: select.grade,
                        pubDepartmentId: select.department,
                        pubMajorId: select.major,
                        pubClassId: select.class
                    })
                }).then((result) => {
                    this.$store.commit('loading', false)            //修改状态
                    this.$store.commit('badNetwork', false)         //修改状态
                    this.$store.commit('nextStep', 5)               //告知前往第五步
                    this.$router.replace('emitFinish')              //告知路由跳转至结束
                    this.$message({
                        message: '发布成功',
                        type: 'success'
                    })
                }).catch((err) => {
                    this.$store.commit('loading', false)            //修改状态
                    this.$store.commit('badNetwork', true)          //修改状态
                    this.$message({
                        message: '发布失败' + err.message,
                        type: 'info'
                    })
                });
            }
        },
        mounted() {
            this.conInfo = this.$store.state.conInfo
            let select = this.$store.state.userSelected //用户选择
            let form = this.$store.state.form           //所有内容
            console.log('userSelected', select);
            console.log('form', form);                  //form是服务器请求的所有列表，select是用户选中的列表
            for (let i = 0; i < select.grade.length; i++) {
                for (let j = 0; j < form.grade.length; j++) {
                    if (select.grade[i] == form.grade[j].gradeId) {
                        this.select.grade.push(form.grade[j].gradeName)
                        console.log('即将push', form.grade[j].gradeName);
                    }
                }
            }
            for (let i = 0; i < select.department.length; i++) {
                for (let j = 0; j < form.department.length; j++) {
                    if (select.department[i] == form.department[j].departmentId) {
                        this.select.department.push(form.department[j].departmentName)
                    }
                }
            }
            if (!this.$store.state.isEmitAll) {
                for (let i = 0; i < select.major.length; i++) {
                    for (let j = 0; j < form.major.length; j++) {
                        if (select.major[i] == form.major[j].majorId) {
                            this.select.major.push(form.major[j].majorName)
                        }
                    }
                }
                for (let i = 0; i < select.class.length; i++) {
                    for (let j = 0; j < form.class.length; j++) {
                        if (select.class[i] == form.class[j].classId) {
                            this.select.class.push(form.class[j].className)
                        }
                    }
                }
            }
        },
    }
</script>
<style scoped>
    .button-group-emit {
        margin-top: 10px;
    }
</style>